﻿#!/usr/bin/python
#-*- coding: utf-8 -*-

import mysql_helper
import sqlite3_helper
from utils import find
from config import db_config
from blog import *

def create_connection_source():
	return sqlite3_helper.Connection.create(db_config['dbfile'])

def create_connection_destination():
	mysql_config = {
		'server': db_config['host'],
		'dbname': db_config['database'],
		'user': db_config['user'],
		'pwd': db_config['pass'],
		'auto_connect': True
	}
	return mysql_helper.Connection.create(**mysql_config)

def start():
	# read users
	if 'passhash' not in BlogUser.fields:
		fields = list(BlogUser.fields)
		fields.append('passhash')
		BlogUser.fields = tuple(fields)
	Service.create_conn = staticmethod(create_connection_source)
	service = BlogService()
	users = service.get_users()
	new_users = [user.duplicate(id_included=True) for user in users]
	for user in new_users:
		Service.create_conn = staticmethod(create_connection_destination)
		user.oldid = user.id
		user.id = service.create_blog({'username':user.username, 
		'namealias':user.namealias, 'email':user.email, 
		'fullname':user.fullname, 'passhash':user.passhash, 
		'domainname':user.domainname, 'createdate':user.createdate, 
		'theme':user.theme, 'status':user.status, 'isroot':user.isroot, 
		'lastlogindate':user.lastlogindate})
		print user.id, user.username, 'created.'
		Service.create_conn = staticmethod(create_connection_source)
		links = user.get_blog().get_links()
		if links:
			print '%s links copied.' % len(links)
		for l in links:
			Service.create_conn = staticmethod(create_connection_destination)
			l.userid = user.id
			l.duplicate().save()
		
	get_new_userid = lambda userid: find(new_users, lambda user: user.oldid==userid).id
	# read all posts
	Service.create_conn = staticmethod(create_connection_source)
	src_posts, more_items = service.get_posts(page=0, pagesize=0, only_published=False, order_desc=False)
	print len(src_posts), 'posts.'
	for p in src_posts:
		p.old_userid = p.userid
		p.userid = get_new_userid(p.userid)
	Service.create_conn = staticmethod(create_connection_destination)
	new_posts = [p.duplicate(id_included=True) for p in src_posts]
	for p in new_posts:
		p.oldid = p.id
		p.id = None
		try:
			p.save()
		except Exception, e:
			print e
			print u'failed to save "%s".' % p.subject
	print 'all %s posts are copied.' % len(new_posts)
	get_new_postid = lambda postid: find(new_posts, lambda p: p.oldid==postid).id
	# read comments
	Service.create_conn = staticmethod(create_connection_source)
	src_comments, more_items = service.get_comments(page=0, pagesize=0, only_published=False, order_desc=False)
	print '%s comments.' % len(src_comments)
	comment_wrote_count = 0
	Service.create_conn = staticmethod(create_connection_destination)
	for c in src_comments:
		c.userid = get_new_userid(c.userid)
		c.postid = get_new_postid(c.postid)
		c.id = None
		c.save()
		comment_wrote_count += 1
		if comment_wrote_count % 100 ==0:
			print comment_wrote_count, 'comments copied. %s left...' % (len(src_comments)-comment_wrote_count)
	print 'all done!'

if __name__ == '__main__':
	start()